package com.rookie.im.minio;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import io.minio.GetObjectArgs;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
import io.minio.http.Method;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class MinioUtils {
	
	private MinioClient minioClient;
	private MiniConfig miniConfig;

	public MinioUtils(MinioClient minioClient, MiniConfig miniConfig) {
		this.minioClient = minioClient;
		this.miniConfig = miniConfig;
	}



	/**
	 * 获取文件url
	 * @param filename 文件名
	 * @param expires 过期时间,单位秒
	 * @return
	 */
	public String fileUrl(String filename, int expires) {
		String url = null;
		
		try {
			return minioClient.getPresignedObjectUrl(
					GetPresignedObjectUrlArgs.builder()
					.bucket(miniConfig.getBucketName())
					.method(Method.GET).expiry(expires)
					.object(filename).build());
		} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException
				| InvalidResponseException | NoSuchAlgorithmException | XmlParserException | ServerException
				| IllegalArgumentException | IOException e) {
			log.error(e.getMessage());
		}
		
		return url;
	}
	/**
	 * 下载文件
	 * @param filename 文件名
	 * @return
	 */
	public ResponseEntity<byte[]> download(String filename) {
		InputStream in = null;
		ByteArrayOutputStream out = null;
		try {
			in = minioClient.getObject(
					GetObjectArgs.builder()
						.bucket(miniConfig.getBucketName())
						.object(filename).build());
			out = new ByteArrayOutputStream();
			IOUtils.copy(in, out);
			byte[] bytes = out.toByteArray();
			HttpHeaders headers = new HttpHeaders();
			headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
			headers.setContentLength(bytes.length);
			headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
			headers.setAccessControlAllowHeaders(Arrays.asList("*"));
			ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
			return responseEntity;
		} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException
				| InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException
				| IllegalArgumentException | IOException e) {
			log.error(e.getMessage());
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				log.error(e.getMessage());
			}
		}
		return null;
	}
}
